//
//  ContractListView.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/2/6.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "ContractListView.h"
#import "ContractListCollectionViewCell.h"
#import "CommodityDetailsViewController.h"
@implementation ContractListView
-(void)creatView{
    self.backgroundColor=_LightGrey;
    _order=0;
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(NAV_HEIGHT);
        make.height.mas_equalTo(50);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    _view1=[UIView new];
    [topView addSubview:_view1];
    [_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/3);
    }];
    
    UITapGestureRecognizer *price = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPrice:)];
    [_view1 addGestureRecognizer:price];
    
    CGSize size1=[ZQTools getNSStringWeight:@"价格" andViewWight:20 andFont:15];
    
    _label1=[UILabel new];
    [_view1 addSubview:_label1];
    [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        if (size1.width>_mainW/3*0.7) {
            make.width.mas_equalTo(_mainW/3*0.7);
        }
        else{
            make.width.mas_equalTo(size1.width);
        }
    }];
    _label1.text=@"价格";
    _label1.textColor=ColorWithRGB(149, 149, 149);
    _label1.font=[UIFont systemFontOfSize:15];
    
    _imageView1=[UIImageView new];
    [_view1 addSubview:_imageView1];
    [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_label1.mas_right).mas_equalTo(5);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(7);
    }];
    _imageView1.image=[UIImage imageNamed:@"向下选择"];
    
    
    _view2=[UIView new];
    [topView addSubview:_view2];
    [_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_view1.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/3);
    }];
    UITapGestureRecognizer *leiXing = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectLeiXing:)];
    [_view2 addGestureRecognizer:leiXing];
    
    
    CGSize size2=[ZQTools getNSStringWeight:@"类型" andViewWight:20 andFont:15];
    
    _label2=[UILabel new];
    [_view2 addSubview:_label2];
    [_label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        if (size2.width>_mainW/3*0.7) {
            make.width.mas_equalTo(_mainW/3*0.7);
        }
        else{
            make.width.mas_equalTo(size2.width);
        }
    }];
    _label2.text=@"类型";
    _label2.textColor=ColorWithRGB(149, 149, 149);
    _label2.font=[UIFont systemFontOfSize:15];
    
    _imageView2=[UIImageView new];
    [_view2 addSubview:_imageView2];
    [_imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_label2.mas_right).mas_equalTo(5);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(7);
    }];
    _imageView2.image=[UIImage imageNamed:@"向下选择"];
    
    
    _view3=[UIView new];
    [topView addSubview:_view3];
    [_view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_view2.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/3);
    }];
    UITapGestureRecognizer *xiaoLiang = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectXiaLiang:)];
    [_view3 addGestureRecognizer:xiaoLiang];
    
    CGSize size3=[ZQTools getNSStringWeight:@"销量" andViewWight:20 andFont:15];
    
    _label3=[UILabel new];
    [_view3 addSubview:_label3];
    [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        if (size3.width>_mainW/3*0.7) {
            make.width.mas_equalTo(_mainW/3*0.7);
        }
        else{
            make.width.mas_equalTo(size3.width);
        }
    }];
    _label3.text=@"销量";
    _label3.textColor=ColorWithRGB(149, 149, 149);
    _label3.font=[UIFont systemFontOfSize:15];
    
    _imageView3=[UIImageView new];
    [_view3 addSubview:_imageView3];
    [_imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_label3.mas_right).mas_equalTo(5);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(7);
    }];
    _imageView3.image=[UIImage imageNamed:@"向下选择"];
    
    for (int i=1; i<3; i++) {
        UIView *xianView=[UIView new];
        [topView addSubview:xianView];
        [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_mainW/3*i);
            make.width.mas_equalTo(1);
            make.top.bottom.mas_equalTo(0);
        }];
        xianView.backgroundColor=_LightGrey;
    }
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT+50, _mainW, _mainH-NAV_HEIGHT-50) collectionViewLayout:flowLayout];
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    
    //注册Cell，必须要有
    [self.collectionView registerClass:[ContractListCollectionViewCell class] forCellWithReuseIdentifier:@"ContractListCollectionViewCell"];
    
    [self addSubview:self.collectionView];
    _page=1;
    
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_collectionView.mj_footer endRefreshing];
        [_collectionView.mj_header endRefreshing];
        [_collectionArr removeAllObjects];
        _page=1;
        [self loadData];
    }];
    
    [self loadData];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionArr.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ContractListCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ContractListCollectionViewCell" forIndexPath:indexPath];
    
    cell.Dic=_collectionArr[indexPath.row];
    [cell creatView];
    return cell;
    
}


//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(_mainW/2-15, _mainH/2.5);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CommodityDetailsViewController *xiangQing=[[CommodityDetailsViewController alloc] init];
    xiangQing.dic=_collectionArr[indexPath.row];
    [ZQTools pushNextViewController:self.viewController andRootController:xiangQing];
    
}



-(void)selectPrice:(UITapGestureRecognizer *)tap{
    
   
    _tableArr=[NSArray arrayWithObjects:@"价格",@"价格由高到低",@"价格由低到高", nil];
    _view=[UIView new];
    [self addSubview:_view];
    [_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    _view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UIView *view=[UIView new];
    [_view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH*0.4);
        make.left.mas_equalTo(0);
    }];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.cornerRadius=4;
    
    BaseTableView *tableView=[BaseTableView new];
    [view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    tableView.tag=1;
    tableView.delegate=self;
    tableView.dataSource=self;
    
}

-(void)selectLeiXing:(UITapGestureRecognizer *)tap{
    
    NSNumber *gtype_id=[_dic objectForKey:@"gtype_id"];
    NSString *gtype_id2=[NSString stringWithFormat:@"%@",gtype_id];
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:gtype_id2,@"gtype_id", nil];
    [ZQTools AFNPostDataUrl:@"user/goods/type_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        
        NSMutableArray *typeArr=[NSMutableArray arrayWithCapacity:0];
        [typeArr addObject:@"类型"];
        NSArray *typeArr2=responseObject;
        
        for (int i=0; i<typeArr2.count; i++) {
            NSDictionary *dic=typeArr2[i];
            [typeArr addObject:[dic objectForKey:@"gtype_name"]];
        }
        _tableArr=typeArr;
        _tableArr2=responseObject;
        
       
        
        _view=[UIView new];
        [self addSubview:_view];
        [_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(_mainW);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(0);
        }];
        _view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        
        UIView *view=[UIView new];
        [_view addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(64);
            make.width.mas_equalTo(_mainW);
            make.height.mas_equalTo(_mainH*0.4);
            make.left.mas_equalTo(0);
        }];
        view.backgroundColor=[UIColor whiteColor];
        view.layer.cornerRadius=4;
        
        BaseTableView *tableView=[BaseTableView new];
        [view addSubview:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
        tableView.tag=2;
        tableView.delegate=self;
        tableView.dataSource=self;
        
    } anderrorBlock:nil];
    
    
}

-(void)selectXiaLiang:(UITapGestureRecognizer *)tap{
    
   
    _tableArr=[NSArray arrayWithObjects:@"销量",@"销量由高到低",@"销量由低到高", nil];
    _view=[UIView new];
    [self addSubview:_view];
    [_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    _view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UIView *view=[UIView new];
    [_view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH*0.4);
        make.left.mas_equalTo(0);
    }];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.cornerRadius=4;
    
    BaseTableView *tableView=[BaseTableView new];
    [view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    tableView.tag=3;
    tableView.delegate=self;
    tableView.dataSource=self;
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] init];
    cell.textLabel.text=_tableArr[indexPath.row];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font=[UIFont systemFontOfSize:18];
    cell.textLabel.textColor=ColorWithRGB(49, 49, 49);
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_view removeFromSuperview];
    [_collectionArr removeAllObjects];
    
    if (tableView.tag==1) {
        if (indexPath.row==0) {
            _order=0;
        } if (indexPath.row==1) {
            _order=2;
        } if (indexPath.row==2) {
            _order=1;
        }
        
        [ZQTools cleanViewSubviews:_view1];
        NSString *Str=_tableArr[indexPath.row];
        CGSize size1=[ZQTools getNSStringWeight:Str andViewWight:20 andFont:15];
        
        _label1=[UILabel new];
        [_view1 addSubview:_label1];
        [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            if (size1.width>_mainW/3*0.7) {
                make.width.mas_equalTo(_mainW/3*0.7);
            }
            else{
                make.width.mas_equalTo(size1.width);
            }
        }];
        _label1.text=Str;
        _label1.textColor=ColorWithRGB(149, 149, 149);
        _label1.font=[UIFont systemFontOfSize:15];
        
        _imageView1=[UIImageView new];
        [_view1 addSubview:_imageView1];
        [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_label1.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(7);
        }];
        _imageView1.image=[UIImage imageNamed:@"向下选择"];
        
        [_collectionArr removeAllObjects];
        _page=1;
        [self loadData];
    }
    
    if (tableView.tag==2) {
        if (indexPath.row==0) {
            [_collectionArr removeAllObjects];
            _selectDic=nil;
            [self loadData];
            return;
        }
        _selectDic=_tableArr2[indexPath.row-1];
        [ZQTools cleanViewSubviews:_view2];
        NSString *Str=_tableArr[indexPath.row];
        CGSize size1=[ZQTools getNSStringWeight:Str andViewWight:20 andFont:15];
        
        _label2=[UILabel new];
        [_view2 addSubview:_label2];
        [_label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            if (size1.width>_mainW/3*0.7) {
                make.width.mas_equalTo(_mainW/3*0.7);
            }
            else{
                make.width.mas_equalTo(size1.width);
            }
        }];
        _label2.text=Str;
        _label2.textColor=ColorWithRGB(149, 149, 149);
        _label2.font=[UIFont systemFontOfSize:15];
        
        _imageView2=[UIImageView new];
        [_view2 addSubview:_imageView2];
        [_imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_label2.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(7);
        }];
        _imageView2.image=[UIImage imageNamed:@"向下选择"];
        [_collectionArr removeAllObjects];
        _page=1;
        [self loadData];
    }

    
    if (tableView.tag==3) {
        if (indexPath.row==0) {
            _order=0;
        } if (indexPath.row==1) {
            _order=3;
        } if (indexPath.row==2) {
            _order=4;
        }
        [ZQTools cleanViewSubviews:_view3];
        NSString *Str=_tableArr[indexPath.row];
        CGSize size1=[ZQTools getNSStringWeight:Str andViewWight:20 andFont:15];
        
        _label3=[UILabel new];
        [_view3 addSubview:_label3];
        [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            if (size1.width>_mainW/3*0.7) {
                make.width.mas_equalTo(_mainW/3*0.7);
            }
            else{
                make.width.mas_equalTo(size1.width);
            }
        }];
        _label3.text=Str;
        _label3.textColor=ColorWithRGB(149, 149, 149);
        _label3.font=[UIFont systemFontOfSize:15];
        
        _imageView3=[UIImageView new];
        [_view3 addSubview:_imageView3];
        [_imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_label3.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(7);
        }];
        _imageView3.image=[UIImage imageNamed:@"向下选择"];
        [_collectionArr removeAllObjects];
        _page=1;
        [self loadData];
    }
}

-(void)loadData{
    NSNumber *gtype_one_id=[_dic objectForKey:@"gtype_id"];
    NSString *gtype_one_id2=[NSString stringWithFormat:@"%@",gtype_one_id];
    
    NSString *gtype_id2=@"0";
    if (_selectDic!=nil) {
        NSNumber *gtype_id=[_selectDic objectForKey:@"gtype_id"];
        gtype_id2=[NSString stringWithFormat:@"%@",gtype_id];
    }
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:gtype_one_id2,@"gtype_one_id",gtype_id2,@"gtype_id",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",_page],@"page",@"5",@"pagesize",[NSString stringWithFormat:@"%d",_order],@"order",@"",@"keywords", nil];
    [ZQTools AFNPostDataUrl:@"user/goods/goods_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        
     
        
        NSArray *typeArr=responseObject;
        [_collectionArr addObjectsFromArray:typeArr];
        [_collectionView reloadData];
        
        if (typeArr.count!=0) {
            _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [_collectionView.mj_footer endRefreshing];
                [_collectionView.mj_header endRefreshing];
                _page=_page+1;
                [self loadData];
            }];
        }
        else{
            _collectionView.mj_footer.hidden=YES;
            
        }
        
    } anderrorBlock:nil];
    
}
#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self ];
    if (touchPoint.y>64+_mainH*0.4) {
        [_view removeFromSuperview];
    }
    
    
}

@end

